############ Fig 1: Plot Figure ###############

library(pacman)
p_load(tidyverse, magrittr, readstata13, ggpubr)
theme_set(theme_classic())
data = read.dta13("estimates/ster/Dube_Demographics.dta") %>%
  mutate(spec = ifelse(spec==4, 3, spec),
         year=2013) %>%
  bind_rows(read.dta13("estimates/ster/Sensitivity_Yearly_Demographics.dta"))


s = 3
yr = 2020

########### Main ################
for (s in c(1,2,3)) {
for (yr in c(2013, 2014, 2017, 2020)) { 
  data_plot = data %>%
    filter(spec==s & year==yr) %>%
    mutate(conflow1 = (beta1 - 1.96*sd1)/mean,
           confhigh1 = (beta1 + 1.96*sd1)/mean,
           conflow2 = (beta2 - 1.96*sd2)/mean,
           confhigh2 = (beta2 + 1.96*sd2)/mean,
           beta1 = beta1/mean,
           beta2 = beta2/mean
           ) 
  
  plot = ggplot(data_plot, aes(x=d)) +
    geom_hline(yintercept=0, linewidth=1.25, color="red", linetype="dashed") + 
    geom_pointrange(aes(y=beta1, ymin=conflow1, ymax=confhigh1), color="gray50", size=1.1) +
    geom_point(aes(y=beta1), color="black", size=4) +
    labs(x='', y="Estimated Elasticity") +
    scale_x_continuous(breaks=c(1:8),
                       label=c("Non-Elderly\nAges <65", "All Ages\nIncluding\nElderly",
                               "Working Age\nAdults\nAges 16-64", "HS Degree\nAges <65",
                               "< HS Degree\n Ages 16-24",	
                               "Single\nMothers", "Black or Hispanic \n Ages < 65", "Children \n Ages < 16")) +
    theme(text=element_text(size=18))
  ggsave(paste0("figures/coef_plot/Demographics_SR", yr, "_", s, ".png"), plot, width=12, height=8)
  
  
  plot = ggplot(data_plot, aes(x=d)) +
    geom_hline(yintercept=0, linewidth=1.25, color="red", linetype="dashed") + 
    geom_pointrange(aes(y=beta2, ymin=conflow2, ymax=confhigh2), color="gray50", size=1.1) +
    geom_point(aes(y=beta2), color="black", size=4) +
    labs(x='', y="Estimated Elasticity") +
    scale_x_continuous(breaks=c(1:8),
                       label=c("Non-Elderly\nAges <65", "All Ages\nIncluding\nElderly",
                               "Working Age\nAdults\nAges 16-64", "HS Degree\nAges <65",
                               "< HS Degree\n Ages 16-24",	
                               "Single\nMothers", "Black or Hispanic \n Ages < 65", "Children \n Ages < 16")) +
    theme(text=element_text(size=18))
  
  ggsave(paste0("figures/coef_plot/Demographics_LR", yr, "_", s, ".png"), plot, width=12, height=8)
  
}
}


######## Synth DD #########

mean <- read.dta13("data/primary/state_panel_mw_poverty_84_w_dem.dta") %>% 
  filter(year<=2013) %>%
  summarize(across(under_1:under_8, ~mean(.)))
yr = 2013
data_plot = tibble() 
for (d in c(1:8)) {
  data_plot = read_csv(paste0("other/synth_overall_ATT_wi_paper_", d,  "_", yr, ".csv")) %>% 
    filter(cut=="high" & full==0) %>%
    mutate(d = d,
           mean = mean[,1],
           conflow1 = (lower)/mean,
           confhigh1 = (upper)/mean,
           beta1 = beta,
           coef = coef*100
    )  %>%
    bind_rows(data_plot)
}
  
for (i in c(75, 100, 50)) {
  plot = ggplot(data_plot %>% filter(coef==i), aes(x=d)) +
    geom_hline(yintercept=0, linewidth=1.25, color="red", linetype="dashed") + 
    geom_pointrange(aes(y=beta1, ymin=conflow1, ymax=confhigh1), color="gray50", size=1.1) +
    geom_point(aes(y=beta1), color="black", size=4) +
    labs(x='', y="Estimated Change in Prob of Poverty from MW") +
    scale_x_continuous(breaks=c(1:8),
                       label=c("Non-Elderly\nAges <65", "All Ages\nIncluding\nElderly",
                               "Working Age\nAdults\nAges 16-64", "HS Degree\nAges <65",
                               "< HS Degree\n Ages 16-24",	
                               "Single\nMothers", "Black or Hispanic \n Ages < 65", "Children \n Ages < 16")) +
    theme(text=element_text(size=18))
  ggsave(paste0("figures/coef_plot/Demographics_Synth_", yr, "_", i, ".png"), plot, width=12, height=8)
  
} 

for (s in c(1,2,3)) {
    data_plot = data %>%
      filter(year %in% c(2013, 2015, 2017, 2020)) %>%
      filter(spec==s) %>%
      mutate(conflow1 = (beta1 - 1.96*sd1)/mean,
             confhigh1 = (beta1 + 1.96*sd1)/mean,
             conflow2 = (beta2 - 1.96*sd2)/mean,
             confhigh2 = (beta2 + 1.96*sd2)/mean,
             beta1 = beta1/mean,
             beta2 = beta2/mean,
             lab = paste0("1983-", year-1),
             d = 2.5 + 5*(d-1),
             d = ifelse(year==2013, d-1.5, 
                        ifelse(year==2015, d-0.5,
                               ifelse(year==2017, d+0.5, d+1.5))),
      ) 

    plot = ggplot(data_plot, aes(x=d, color=lab, shape=lab, group=lab)) +
      geom_hline(yintercept=0, linewidth=1.25, color="red", linetype="dashed") +
      geom_pointrange(aes(y=beta1, ymin=conflow1, ymax=confhigh1), size=1.1) +
      geom_point(aes(y=beta1), size=4) +
      labs(x="", y="Estimated Elasticity") +
      scale_color_manual(name="",
                         #values=c("g", "#619CFF", "#00BA38","#C77CFF")
                         values=c("gray80", "gray50", "gray20", "black")
                         ) +
      scale_shape_manual(guide = "legend",
                         name="",
                         values=c("circle", "triangle", "square", "diamond")) +
      theme(text=element_text(size=18),
            plot.margin=unit(c(0.25,0,0.25,0), "cm"),
            plot.title = element_text(hjust = 0.5),
            legend.position="bottom") +
      scale_x_continuous(breaks=c(0:7)*5+2.5,
                         label=c("Non-Elderly\nAges <65", "All Ages\nIncluding\nElderly",
                                 "Working Age\nAdults\nAges 16-64", "HS Degree\nAges <65",
                                 "< HS Degree\n Ages 16-24",
                                 "Single\nMothers", "Black or Hispanic \n Ages < 65", "Children"))

    ggsave(paste0("figures/coef_plot/Demographics_SR_Combined", "_", s, ".png"), plot, width=12, height=8)


    plot = ggplot(data_plot, aes(x=d, color=lab, shape=lab, group=lab)) +
      geom_hline(yintercept=0, linewidth=1.25, color="red", linetype="dashed") +
      geom_pointrange(aes(y=beta2, ymin=conflow2, ymax=confhigh2), size=1.1) +
      geom_point(aes(y=beta2), size=4) +
      labs(x="", y="Estimated Elasticity") +
      scale_color_manual(name="",
                         #values=c("g", "#619CFF", "#00BA38","#C77CFF")
                         values=c("gray80", "gray50", "gray20", "black")) +
      scale_shape_manual(guide = "legend",
                         name="",
                         values=c("circle", "triangle", "square", "diamond")) +
      theme(text=element_text(size=18),
            plot.margin=unit(c(0.25,0,0.25,0), "cm"),
            plot.title = element_text(hjust = 0.5),
            legend.position="bottom") +
      scale_x_continuous(breaks=c(0:7)*5+2.5,
                         label=c("Non-Elderly\nAges <65", "All Ages\nIncluding\nElderly",
                                 "Working Age\nAdults\nAges 16-64", "HS Degree\nAges <65",
                                 "< HS Degree\n Ages 16-24",
                                 "Single\nMothers", "Black or Hispanic \n Ages < 65", "Children"))
    

    ggsave(paste0("figures/coef_plot/Demographics_LR_Combined", "_", s, ".png"), plot, width=12, height=8)


}

